﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using ReaXML;
using System.IO;
using SCH_Tools.Modules;
using SCH_Tools.Forms;

namespace SCH_Tools
{
    public partial class frmAdminDataBase : Form
    {
        XmlGenerate XmlTool = new XmlGenerate();

        #region "Global Parameter's"
        [Description("Server selected for script run")]
        public Server GetServerSelected()
        {
            List<Server> Servers = XmlTool.GetInformationServer(cbServer.Text.Trim());
            return Servers[0];
        }

        [Description("Collections of scriptsfiles found")]
        public List<string> FilesFound = new List<string>();

        [Description("Driver execution Files Collection")]
        public ServerDriver Driver;

        [Description("Path directory output")]
        public string PathOutput
        {
            get
            {
                return txPathOut.Text.Trim();
            }
        }

        [Description("Output Required")]
        public bool ActiveOutput
        {
            get
            {
                return ckGenOut.Checked;
            }
        }
        #endregion

        #region "Starting"
        void LoadConfiguration()
        {
            try
            {
                XmlTool.Initialeze();

                List<Server> CollectionServer = XmlTool.GetListServer();
                cbServer.DataSource = CollectionServer;
                cbServer.DisplayMember = "ServerName";
                cbServer.ValueMember = "ServerName";
                cbServer.Update();
                if (CollectionServer.Count > 0)
                {
                    cbServer.SelectedIndex = 0;
                    cbServer.Text = CollectionServer[0].ServerName;
                }

                List<ServerDriver> CollectionServerDrv = XmlTool.GetListServerDriver(null);

                cbServerType.DataSource = CollectionServerDrv;
                cbServerType.DisplayMember = "Name";
                cbServerType.ValueMember = "Name";
                cbServerType.Update();

                cbServerDrv.DataSource = CollectionServerDrv;
                cbServerDrv.DisplayMember = "Name";
                cbServerDrv.ValueMember = "Name";
                cbServerDrv.Update();

                cbServerDriver.DataSource = CollectionServerDrv;
                cbServerDriver.ValueMember = "Name";
                cbServerDriver.DisplayMember = "Name";
                cbServerDriver.Update();

                txServerName.Text = string.Empty;
                txServerIp.Text = string.Empty;
                txServerPort.Text = string.Empty;
                txServerUser.Text = string.Empty;
                txServerPass.Text = string.Empty;
                txAlias.Text = string.Empty;
                txDriver.Text = string.Empty;
                txDrvHost.Text = string.Empty;
                txDrvInput.Text = string.Empty;
                txDrvOutput.Text = string.Empty;
                txDrvUser.Text = string.Empty;
                txDrvPass.Text = string.Empty;
                txPathCmd.Text = string.Empty;
                txNameCmd.Text = string.Empty;

                lbServer.Text = string.Empty;
                lbUser.Text = string.Empty;
                lbPass.Text = string.Empty;
                lbType.Text = string.Empty;
                lbAlias.Text = string.Empty;
                lbHostServer.Text = string.Empty;

                if (cbServerDrv.Items.Count > 0)
                {
                    cbServerDrv.SelectedIndex = 0;
                    cbServerDrv_SelectedIndexChanged(null, null);
                }

                if (cbServer.Items.Count > 0)
                {
                    cbServer.SelectedIndex = 0;
                    cbServer_SelectedIndexChanged(null, null);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        #region "Register Server"
        private void btGuardar_Click(object sender, EventArgs e)
        {
            XmlTool.ServerMaintenance(new Server
            {
                ServerName = txServerName.Text,
                Host = txHost.Text,
                Alias = txAlias.Text,
                Ip = txServerIp.Text,
                Port = txServerPort.Text,
                User = txServerUser.Text,
                Pass = txServerPass.Text,
                ServerType = cbServerType.Text.Trim()
            });
            LoadConfiguration();
        }

        private void cbServer_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<Server> Servers = XmlTool.GetInformationServer(cbServer.Text.Trim());
            if (Servers.Count > 0)
            {
                lbHostServer.Text = Servers[0].Host;
                lbAlias.Text = Servers[0].Alias;
                lbServer.Text = string.Concat(Servers[0].Ip, ",", Servers[0].Port);
                lbUser.Text = Servers[0].User;
                lbPass.Text = Servers[0].Pass;
                lbType.Text = Servers[0].ServerType;

                txServerName.Text = cbServer.Text.Trim();
                txHost.Text = Servers[0].Host;
                txAlias.Text = Servers[0].Alias;
                txServerIp.Text = Servers[0].Ip;
                txServerPort.Text = Servers[0].Port;
                txServerUser.Text = Servers[0].User;
                txServerPass.Text = Servers[0].Pass;
                cbServerType.SelectedIndex = cbServerType.FindString(Servers[0].ServerType);
            }
        }

        private void btEliminar_Click(object sender, EventArgs e)
        {
            XmlTool.RemoveServer(new Server
            {
                ServerName = txServerName.Text,
                Ip = txServerIp.Text,
                Port = txServerPort.Text,
                User = txServerUser.Text,
                Pass = txServerPass.Text,
                ServerType = cbServerType.Text.Trim()
            });
            LoadConfiguration();
        }
        #endregion

        #region "Run Selected Script"
        private void btRunSelText_Click(object sender, EventArgs e)
        {
            Main.RunFiles(true);
        }
        #endregion

        #region "Generate out Scripts"
        private void btRunDocument_Click(object sender, EventArgs e)
        {
            //CLEAN VISUAL REGISTERED OF FILES FOUND
            FilesFound.Clear();

            //GET DRIVER SELECTED
            List<ServerDriver> Servers = XmlTool.GetListServerDriver(cbServerDriver.Text.Trim());
            if (Servers.Count > 0)
            {
                Driver = new ServerDriver();
                Driver = Servers[0];
            }

            //OPTIONS
            if (chRecursive.Checked)
            {
                FilesFound.AddRange(Directory.GetFiles(Directory.GetParent(Main.GetFileDocument()).ToString(), "*.*", SearchOption.AllDirectories));
            }
            else
            {
                FilesFound.AddRange(new string[] { Main.GetFileDocument() });
            }

            //RUN SCRIPTS
            Main.RunMassiveFiles();
        }

        private void ckGenOut_CheckedChanged(object sender, EventArgs e)
        {
            txPathOut.Enabled = ckGenOut.Checked;
            btBrowse.Enabled = ckGenOut.Checked;
            txPathOut.Text = string.Empty;
        }

        private void btBrowse_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog browse = new FolderBrowserDialog();
            txPathOut.Text = string.Empty;
            if (browse.ShowDialog() == DialogResult.OK)
            {
                txPathOut.Text = browse.SelectedPath;
            }
        }
        #endregion

        #region "Driver Execution"
        private void cbServerDrv_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<ServerDriver> Servers = XmlTool.GetListServerDriver(cbServerDrv.Text.Trim());
            if (Servers.Count > 0)
            {
                cbServerDrv.Text = Servers[0].Name;
                txDriver.Text = Servers[0].Driver;
                txNameCmd.Text = Servers[0].CmdName;
                txPathCmd.Text = Servers[0].Path;
                //PARAMETER
                txDrvHost.Text = Servers[0].ParamHost;
                txDrvServer.Text = Servers[0].ParamServer;
                txDrvUser.Text = Servers[0].ParamUser;
                txDrvPass.Text = Servers[0].ParamPass;
                txDrvInput.Text = Servers[0].ParamInput;
                txDrvOutput.Text = Servers[0].ParamOutput;

            }
        }

        private void btAddDriver_Click(object sender, EventArgs e)
        {
            XmlTool.ServerCmdMaintenance(new ServerDriver()
            {
                Name = cbServerDrv.Text,
                CmdName = txNameCmd.Text.Trim(),
                Driver = txDriver.Text.Trim(),
                Path = txPathCmd.Text.Trim(),
                //PARAMETER
                ParamHost = txDrvHost.Text.Trim(),
                ParamServer = txDrvServer.Text.Trim(),
                ParamUser = txDrvUser.Text.Trim(),
                ParamPass = txDrvPass.Text.Trim(),
                ParamInput = txDrvInput.Text.Trim(),
                ParamOutput = txDrvOutput.Text.Trim()
            });

            LoadConfiguration();
        }

        private void btBrowserCmd_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            DialogResult result = dialog.ShowDialog(); // Show the dialog.
            if (result == DialogResult.OK) // Test result.
            {
                txPathCmd.Text = dialog.FileName;
            }
        }
        #endregion

        public frmAdminDataBase()
        {
            InitializeComponent();
            LoadConfiguration();
        }
    }
}
